package com.cares.event.utils;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * 事件流工具类
 * @author xuji
 *
 */
public class EventflowUtil {
	
	//存储过程名称Key
	public static String PROCEDURE_NAME = "procedureName";
	
	//存储过程In参数Key
	public static String PROCEDURE_IN = "procedureIn";
	
	//存储过程out参数Key
	public static String PROCEDURE_OUT = "procedureOut";
	
	
	
    /**
     * 解析存储过程表达式
     * 样本： callUpdateUser('cares',100,Date||2013-7-2:50-01-01,true,out||result);
     * 其中in参数格式标准：字符串：'***', 数字和布尔型：***，日期型：Date||2013-7-2:50-01-01
     * out||result为out参数，result与数据库中的out参数名称一致
     * 返回 Map :"prName": prName,"inParam",:params,"outParam":outParamName
     * @throws ParseException 
     */
	public static Map<String, Object> readProcedureFullName(String proFullName){
		Map<String, Object> result = new HashMap<String, Object>();
		//String er = "callUpdateUser('cares','000',)";
		//存储过程名称
		//String prName = proFullName.
		Pattern pattern = Pattern.compile("\\w*(\\w*)");
		Matcher matcher = pattern.matcher(proFullName);
		if(!matcher.find()){//如果不匹配
			return result;
		}else{
			//存储过程名称
			String prName = proFullName.substring(0, proFullName.indexOf("("));
			result.put(PROCEDURE_NAME, prName);
			//存储过程参数
			String prParam = proFullName.substring(proFullName.indexOf("(")+1, proFullName.indexOf(")"));
			if(!StringUtils.isBlank(prParam)){
				String[] temps = prParam.split(",");
				//返回结果
				Object[] params = null;
				if(prParam.contains("out||")){//包含返回结果
					params = new Object[temps.length-1];
				}else{
					params = new Object[temps.length];
				}
				String outParamName = ""; 
				for(int i=0; i<temps.length; i++){
					if(temps[i].trim().startsWith("'") && temps[i].trim().endsWith("'")){ //字符串类型的IN参数
						params[i] = temps[i].trim().substring(1, temps[i].trim().length()-1);
					}else if(temps[i].trim().startsWith("out||")){//存储过程OUT参数
						outParamName += "," + temps[i].trim().substring(5);
					}else if(temps[i].trim().startsWith("Date||")){ //时间格式的参数
						Date dt = null;
						try{
							if(temps[i].trim().substring(6).length() <= 7){
								dt = DateUtil.getStrDate(temps[i].trim().substring(6), DateUtil.DATE_FORMAT_YM);
							}else if(7<temps[i].trim().substring(6).length() &&
									 	temps[i].trim().substring(6).length()<10){
								dt = DateUtil.getStrDate(temps[i].trim().substring(6), DateUtil.DATE_FORMAT_YMD);
							}else{
								dt = DateUtil.getStrDate(temps[i].trim().substring(6), DateUtil.DATE_FORMAT_LONG);
							}
							params[i] = dt;
						} catch(ParseException e){
							e.printStackTrace();
						}
					}else{ //布尔型和数字型
						if("true".equals(temps[i].trim()) || "false".equals(temps[i].trim())){
							params[i] = new Boolean(temps[i].trim());
						}else{
							params[i] = new Double(temps[i].trim());
						}
					}
				}
				if(params.length>0){
					result.put(PROCEDURE_IN, params);
				}
				if(StringUtils.isNotBlank(outParamName)){
					result.put(PROCEDURE_OUT, outParamName.substring(1).split(","));
				}
			}
			return result;
		}
	}


}
	
	
